//
//  LineView.m
//  ZXT
//
//  Created by 张逸阳 on 2017/8/15.
//  Copyright © 2017年 dr. All rights reserved.
//

#import "LineView.h"
#import "XAxisView.h"
#import "YAxisView.h"


#define leftMargin 45
//#define defaultSpace 5
#define lastSpace 50

@interface LineView()
@property (strong, nonatomic) NSArray *xTitleArray;

///折线1
@property (strong, nonatomic) NSArray *yValueArray;
///折线2
@property (strong, nonatomic) NSArray *yValueArrayA;
///折线3
@property (strong, nonatomic) NSArray *yValueArrayB;



@property (assign, nonatomic) CGFloat yMax;
@property (assign, nonatomic) CGFloat yMin;

@property (assign, nonatomic) CGFloat defaultSpace;//间距
@property (assign, nonatomic) CGFloat pointGap;//手指移动间距

@property (strong, nonatomic) YAxisView *yAxisView;
@property (strong, nonatomic) XAxisView *xAxisView;
@property (assign, nonatomic) CGFloat moveDistance;



@end

@implementation LineView

- (instancetype)initWithFrame:(CGRect)frame xTitleArray:(NSArray *)xTitleArray yValueArray:(NSArray *)yValueArray yValueArrayA:(NSArray *)yValueArrayA yValueArrayB:(NSArray *)yValueArrayB yMax:(CGFloat)yMax yMin:(CGFloat)yMin
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.xTitleArray = xTitleArray;
        self.yValueArray = yValueArray;
        self.yMax = yMax;
        self.yMin = yMin;

        
        if (xTitleArray.count > 600) {
            _defaultSpace = 5;
        }
        else if (xTitleArray.count > 400 && xTitleArray.count <= 600){
            _defaultSpace = 10;
        }
        else if (xTitleArray.count > 200 && xTitleArray.count <= 400){
            _defaultSpace = 20;
        }
        else if (xTitleArray.count > 100 && xTitleArray.count <= 200){
            _defaultSpace = 30;
        }
        else {
            _defaultSpace = 40;
        }
        
        
        self.yValueArrayA = yValueArrayA;
        self.yValueArrayB = yValueArrayB;
        
        [self creatYAxisView];
        [self creatXAxisView];
        
        //长按手势
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(event_longPressAction:)];
        [self.xAxisView addGestureRecognizer:longPress];

        //拖拽
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(event_longPressAction:)];
        [self.xAxisView addGestureRecognizer:pan];
        
    }
    return self;
}


- (void)event_longPressAction:(UILongPressGestureRecognizer *)longPress
{
    
    if(UIGestureRecognizerStateChanged == longPress.state || UIGestureRecognizerStateBegan == longPress.state) {
        
        CGPoint location = [longPress locationInView:self.xAxisView];
        
        //相对于屏幕的位置
        CGPoint screenLoc = CGPointMake(location.x - self.frame.origin.x, location.y);
        [self.xAxisView setScreenLoc:screenLoc];
        
        if (ABS(location.x - _moveDistance) > self.pointGap) { //不能长按移动一点点就重新绘图  要让定位的点改变了再重新绘图
            
            [self.xAxisView setIsShowLabel:YES];
            [self.xAxisView setIsLongPress:YES];
            self.xAxisView.currentLoc = location;
            _moveDistance = location.x;
        }
    }
    
    if(longPress.state == UIGestureRecognizerStateEnded)
    {
        _moveDistance = 0;
        //恢复scrollView的滑动
        [self.xAxisView setIsLongPress:NO];
        [self.xAxisView setIsShowLabel:NO];
        
    }
}

- (void)creatYAxisView
{
    self.yAxisView = [[YAxisView alloc] initWithFrame:CGRectMake(0, 0, leftMargin, self.frame.size.height) yMax:self.yMax yMin:self.yMin];
    
    [self addSubview:self.yAxisView];
}

- (void)creatXAxisView
{
    self.xAxisView = [[XAxisView alloc] initWithFrame:CGRectMake(leftMargin, 0, self.frame.size.width-leftMargin, self.frame.size.height) xTitleArray:self.xTitleArray yValueArray:self.yValueArray yMax:self.yMax yMin:self.yMin];
        
    
    self.xAxisView.yValueArrayA = self.yValueArrayA;
    self.xAxisView.yValueArrayB = self.yValueArrayB;
    [self addSubview:self.xAxisView];
    
}

- (void)drawRect:(CGRect)rect
{
    // Drawing code
}




@end
